package com.erp.fin.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.erp.common.utils.ShiroUtils;
import com.erp.fin.domain.PricetypeDTO;
import com.erp.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.fin.domain.PricetypeDO;
import com.erp.fin.service.PricetypeService;
import com.erp.common.utils.PageUtils;
import com.erp.common.utils.Query;
import com.erp.common.utils.R;

/**
 * 费用类型
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-07-31 13:56:20
 */
 
@Controller
@RequestMapping("/fin/pricetype")
public class PricetypeController {
	@Autowired
	private PricetypeService pricetypeService;
	
	@GetMapping()
	@RequiresPermissions("fin:pricetype:pricetype")
	String Pricetype(){
	    return "fin/pricetype/pricetype";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("fin:pricetype:pricetype")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<PricetypeDO> pricetypeList = pricetypeService.list(query);
		int total = pricetypeService.count(query);
		PageUtils pageUtils = new PageUtils(pricetypeList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("fin:pricetype:add")
	String add(){
	    return "fin/pricetype/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("fin:pricetype:edit")
	String edit(@PathVariable("id") Long id,Model model){
		PricetypeDO pricetype = pricetypeService.get(id);
		model.addAttribute("pricetype", pricetype);
	    return "fin/pricetype/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("fin:pricetype:add")
	public R save( PricetypeDO pricetype){
		UserDO userDO = ShiroUtils.getUser();
		pricetype.setGmtCreate(new Date());
		pricetype.setUserIdCreate(userDO.getUserId());
		pricetype.setUserNameCreate(userDO.getName());
		if(pricetypeService.save(pricetype)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("fin:pricetype:edit")
	public R update( PricetypeDO pricetype){
		pricetypeService.update(pricetype);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("fin:pricetype:remove")
	public R remove( Long id){
		if(pricetypeService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("fin:pricetype:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		pricetypeService.batchRemove(ids);
		return R.ok();
	}


	/**
	 * 费用类型列表
	 * @return
	 */
	@ResponseBody
	@GetMapping("/priceTypelist")
	public List<PricetypeDTO> priceTypelist(){
		//查询列表数据
		List<PricetypeDO> pricetypeList = pricetypeService.list(null);

		List<PricetypeDTO> pricetypeDTOList = new ArrayList<>();
		for (PricetypeDO pricetype:pricetypeList) {
			PricetypeDTO pricetypeDTO = new PricetypeDTO();
			pricetypeDTO.setId(pricetype.getId());
			pricetypeDTO.setName(pricetype.getPrictType());
			pricetypeDTOList.add(pricetypeDTO);
		}
		return pricetypeDTOList;
	}
}
